<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师编号" prop="teacherNo">
      <el-input v-model="dataForm.teacherNo" placeholder="教师编号"></el-input>
    </el-form-item>
    <el-form-item label="教师名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="教师名称"></el-input>
    </el-form-item>
    <el-form-item label="教师性别" prop="gender">
      <el-select v-model="dataForm.gender" placeholder="请选择">
        <el-option
          v-for="item in genderOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="教师籍贯" prop="nativePlace">
      <el-input v-model="dataForm.nativePlace" placeholder="教师籍贯"></el-input>
    </el-form-item>
    <el-form-item label="健康状况" prop="health">
      <el-select v-model="dataForm.health" placeholder="请选择">
        <el-option
          v-for="item in healthOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="政治面貌" prop="polity">
      <el-select v-model="dataForm.polity" placeholder="请选择">
        <el-option
          v-for="item in polityOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="教师联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="教师联系电话"></el-input>
    </el-form-item>
    <el-form-item label="家庭详细地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="家庭详细地址"></el-input>
    </el-form-item>
    <el-form-item label="简介" prop="resume">
      <el-input v-model="dataForm.resume" placeholder="简介"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        genderOptions: [
          {
            id: 0,
            name: '男'
          },
          {
            id: 1,
            name: '女'
          }
        ],
        healthOptions: [
          {
            id: 0,
            name: '健康'
          },
          {
            id: 1,
            name: '不健康'
          }
        ],
        polityOptions: [
          {
            id: 0,
            name: '群众'
          },
          {
            id: 1,
            name: '共青团员'
          },
          {
            id: 2,
            name: '中共预备党员'
          },
          {
            id: 3,
            name: '中共预员'
          }
        ],
        visible: false,
        dataForm: {
          id: 0,
          teacherNo: '',
          name: '',
          gender: '',
          nativePlace: '',
          health: '',
          polity: '',
          phone: '',
          address: '',
          resume: '',
          userId: ''
        },
        dataRule: {
          teacherNo: [
            { required: true, message: '教师编号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '教师名称不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '教师性别不能为空', trigger: 'blur' }
          ],
          nativePlace: [
            { required: true, message: '教师籍贯不能为空', trigger: 'blur' }
          ],
          health: [
            { required: true, message: '健康状况不能为空', trigger: 'blur' }
          ],
          polity: [
            { required: true, message: '政治面貌不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '教师联系电话不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '家庭详细地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manage/teacherinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.teacherNo = data.teacherInfo.teacherNo
                this.dataForm.name = data.teacherInfo.name
                this.dataForm.gender = data.teacherInfo.gender
                this.dataForm.nativePlace = data.teacherInfo.nativePlace
                this.dataForm.health = data.teacherInfo.health
                this.dataForm.polity = data.teacherInfo.polity
                this.dataForm.phone = data.teacherInfo.phone
                this.dataForm.address = data.teacherInfo.address
                this.dataForm.resume = data.teacherInfo.resume
                this.dataForm.userId = data.teacherInfo.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/teacherinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'teacherNo': this.dataForm.teacherNo,
                'name': this.dataForm.name,
                'gender': this.dataForm.gender,
                'nativePlace': this.dataForm.nativePlace,
                'health': this.dataForm.health,
                'polity': this.dataForm.polity,
                'phone': this.dataForm.phone,
                'address': this.dataForm.address,
                'resume': this.dataForm.resume,
                'userId': this.dataForm.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
